package com.xjk.shiro.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.context.annotation.Bean;

/**
 * @author: XJK
 */
public class MyRealm2 extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String principal = (String) token.getPrincipal();
        String pwd = String.valueOf((char[]) token.getCredentials());
//        System.out.println(principal);
        String correctUsername="xjk";
        String correctPassword="root";
        String salt = "1qaz2wsx";
        int hashIterations=1024;
        if(correctUsername.equals(principal)){
            return new SimpleAuthenticationInfo(principal,
                    new Md5Hash(correctPassword,salt,hashIterations).toHex(),
                    ByteSource.Util.bytes(salt),
                    this.getName());
        }
        return null;
    }
}
